package com.multi.thread.chapter03.demo07;

/**
 *	如果通知过早，则会打乱程序正常的运行逻辑
 */
public class MyRun {
	
	private String lock = new String("");
	
	private boolean isFirstRun = false;
	
	private Runnable run1 = new Runnable() {
		
		@Override
		public void run() {
			try {
				synchronized (lock) {
					while (isFirstRun == false) {
						System.out.println("begin wait!");
						lock.wait();
						System.out.println("end wait!");
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};
	
	private Runnable run2 = new Runnable() {
		
		@Override
		public void run() {
			synchronized (lock) {
				System.out.println("begin notify!");
				lock.notify();
				System.out.println("end notify!");
				isFirstRun = true;
			}
		}
	};
	
	public static void main(String[] args) throws InterruptedException {
		MyRun myRun = new MyRun();
		Thread t1 = new Thread(myRun.run1);
		t1.start();
		Thread.sleep(100);
		Thread t2 = new Thread(myRun.run2);
		t2.start();
	}

}
